'''
该程序说明：使用点亮一颗 LED
在线文档：https://docs.geeksman.com/esp32/MicroPython/04.esp32-micropython-LED.html
'''
 
import machine  
import time  
  
# 初始化GPIO引脚  
pin_detect = machine.Pin(7, machine.Pin.IN)  # 假设旋转编码器的脉冲检测引脚连接到GPIO 2上  
pin_clk = machine.Pin(8, machine.Pin.OUT)  # 假设旋转编码器的时钟引脚连接到GPIO 4上  
pin_clk.value(0)  # 设置时钟引脚为低电平  
pin_button = machine.Pin(6, machine.Pin.IN)  # 假设按键连接到GPIO 15上  

# 初始化旋转编码器参数  
count = 0  # 脉冲计数器  
prev_state = pin_detect.value()  # 上一个状态  
curr_state = 0  # 当前状态  
direction = 1  # 正向旋转方向，默认为正转  
speed = 100  # 旋转速度，单位为脉冲数/秒

dupCount=0
  
# 循环读取旋转编码器的状态并计数  
while True:
    time.sleep(0.1)
    curr_state = pin_detect.value()  # 读取当前状态
    print("D: %d, C: %d" % (pin_detect.value(), pin_clk.value()))
#     if curr_state != prev_state:  # 如果状态发生变化
#        
#         print("---0----:")
#         if (curr_state != pin_clk.value()):
#             if (dupCount==0):
#                 print("---1----:")
#                 count+= 1 
#                 dupCount+= 1
#             if (dupCount==1):
#                 print("---2----:")
#                 dupCount =0
#         else:
#            count == 1
#            print("---3----:")
#         print("Position:")
#         print(count)
#     prev_state = curr_state
#     print("test")